package com.imay.live.fragment.room;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.R;

/**
 * Created by jeaman on 2017/5/4.
 */

public class LinkMicInviteDialog extends BaseDialogFragment implements View.OnClickListener {
    private TextView mTimeTv;

    private int mCountDownTime = 10;
    private Runnable mCountDownRunnable;

    public static LinkMicInviteDialog newInstance() {
        return new LinkMicInviteDialog();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_link_mic;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTimeTv = $(R.id.link_mic_dialog_tv_time);

        $(R.id.link_mic_dialog_btn_agree).setOnClickListener(this);
        $(R.id.link_mic_dialog_btn_refuse).setOnClickListener(this);

        mCountDownRunnable = new Runnable() {
            @Override
            public void run() {
                if (isAdded() && !isDetached()) {
                    mTimeTv.setText(String.valueOf(--mCountDownTime));
                    if (mCountDownTime > 0) {
                        countDown();
                    } else {
                        dismiss();
                    }
                }
            }
        };
        countDown();
    }

    private void countDown() {
        mTimeTv.postDelayed(mCountDownRunnable, 1000);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mTimeTv.removeCallbacks(mCountDownRunnable);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.link_mic_dialog_btn_refuse:
                dismiss();
                if (getActivity() instanceof OnLinkMicInviteListener) {
                    ((OnLinkMicInviteListener) getActivity()).onRefuseInviteClick();
                }
                break;
            case R.id.link_mic_dialog_btn_agree:
                dismiss();
                if (getActivity() instanceof OnLinkMicInviteListener) {
                    ((OnLinkMicInviteListener) getActivity()).onAgreeInviteClick();
                }
                break;
        }
    }

    public interface OnLinkMicInviteListener {
        void onAgreeInviteClick();
        void onRefuseInviteClick();
    }
}
